﻿using Root.Models;
using Root.Models.BLL;
using Root.Models.DAL;
using Root.Models.Structs;
using System;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

namespace Root.Controllers
{
    public class ArticlesController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.articles = ArticleDAL.GetTitleList();
            string cookie = "";
            try
            {
                cookie = HttpUtility.UrlDecode(Request.Cookies["name"].Value.ToString());
            }
            catch (Exception)
            {
                cookie = "";
            }
            ViewBag.cookie = cookie;
            ViewBag.time = Convert.ToInt32(ConfigHelper.appSettings["timeSpan_01"]);
            return View();
        }

        public ActionResult Show(int id)
        {
            ViewBag.articles = ArticleDAL.GetListByPid(id);
            Article article = ArticleDAL.GetArticleById(id);
            ViewBag.articleFrom = article;
            ViewBag.title = article.Msg;
            ViewBag.pid = article.Id;
            ViewBag.time = Convert.ToInt32(ConfigHelper.appSettings["timeSpan_01"]);
            string cookie = "";
            try
            {
                cookie = HttpUtility.UrlDecode(Request.Cookies["name"].Value.ToString());
            }
            catch (Exception)
            {
                cookie = "";
            }
            ViewBag.cookie = cookie;
            ArticleDAL.AddCount(id);
            return View();
        }

        [ValidateAntiForgeryToken]
        public ActionResult Add()
        {
            string name = Request["name"];
            string msg = HttpUtility.UrlEncode(Request["title"]);
            string ip = Request["ip"];
            string location = CommonDAL.GetLocationByIPAddress(ip);
            string agent = Request.UserAgent;
            int pid;
            try
            {
                pid = Convert.ToInt32(Request["pid"]);
            }
            catch (Exception)
            {
                pid = 0;
            }
            if (pid != 0)
            {
                ArticleDAL.AddCount(pid);
                ArticleDAL.AddCount(pid);
                ArticleDAL.AddCount(pid);
            }
            if (string.IsNullOrEmpty(msg))
            {
                msg = Request.Unvalidated["msg"];
            }

            if (String.IsNullOrEmpty(name) | (String.IsNullOrWhiteSpace(name)))
            {
                name = "匿名用户";
            }
            if (String.IsNullOrEmpty(msg))
            {
                msg = "Null";
            }
            name = AntiXSS.ValidateInput(name, "＜Don't do that＞", "<script>", "</script>", "<iframe>", "</iframe>", "onerror=", "javascript:", "<script", "<iframe");
            msg = AntiXSS.ValidateInput(msg, "＜Don't do that＞", "<script>", "</script>", "<iframe>", "</iframe>", "onerror=", "javascript:", "<script", "<iframe");

            Article article = new Article();
            article.Name = name;
            article.Msg = msg;
            article.IP = ip;
            article.Location = location;
            article.Agent = agent;
            article.Time = DateTime.Now.ToString();
            article.Pid = Convert.ToInt64(pid);
            article.Count = ArticleDAL.GetMAXCount() + 1;
            ArticleDAL.Insert(article);
            HttpCookie name_cookie = new HttpCookie("name", HttpUtility.UrlEncode(name));
            name_cookie.Expires = DateTime.Now.AddYears(1000);
            Response.SetCookie(name_cookie);
            if (pid == 0)
            {
                return RedirectToRoute(new { controller = "Articles", action = "Index" });
            }
            else
            {
                return RedirectToRoute(new { controller = "Articles", action = "Show", id = pid });
            }
        }
    }
}